package com.kazakovs.skype.bot;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Handler {

	public enum Types {
		LINK("http"), PRIVATE("!private"), TWITTER("!twit"), FBPOST("!fbpost"), RENAME(
				"!rename"), HELP("!help");

		private String strType;

		Types(String str) {
			this.strType = str;
		}
	}

	public static Types ty;
	final String XML_PATTERN_STR = "<(\\S+?)(.*?)>(.*?)</\\1>";
	final String LINK_PATTERN_STR = "\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))";

	private Pattern patternTag, patternLink;
	private Matcher matcherTag, matcherLink;
	private static final String HTML_A_TAG_PATTERN = "(?i)<a([^>]+)>(.+?)</a>";
	private static final String HTML_A_HREF_TAG_PATTERN = "\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))";

	/*
	 * public static final String LINK = "http"; public static final String
	 * PRIVATE = "!private"; public static final String TWITTER = "!twit";
	 * public static final String FBPOST = "!fb"; public static final String
	 * RENAME = "!rename"; public static final String HELP = "!help";
	 */
	private String message = null;
	private String handlerType = null;

	private boolean isXmlInString(String str) {

		return Pattern
				.compile(
						XML_PATTERN_STR,
						Pattern.CASE_INSENSITIVE | Pattern.DOTALL
								| Pattern.MULTILINE).matcher(str).matches();

	}

	public Handler(String mess) {
		String type = "";
		setMessage(mess.trim());
		for (Types ty : Types.values()) {
			String stri = ty.strType;
			if (mess.contains(stri)) {
				type += stri + "/";
				if (!(stri.equalsIgnoreCase("http")))
					setMessage(getMessage().replaceAll(ty.strType, "").trim());
			}
		}

		if (isXmlInString(getMessage())) {
			patternTag = Pattern.compile(HTML_A_TAG_PATTERN);
			patternLink = Pattern.compile(HTML_A_HREF_TAG_PATTERN);

			matcherTag = patternTag.matcher(getMessage());

			while (matcherTag.find()) {

				String href = matcherTag.group(1); // href
				setMessage(matcherTag.group(2).contains("http") ? matcherTag
						.group(2) : "http://" + matcherTag.group(2)); 

				matcherLink = patternLink.matcher(href);

				while (matcherLink.find()) {
					String link = matcherLink.group(1); // ссылка
					// result.add(new HtmlLink(link, linkText));
				}
			}

		}
		// this.setMessage(mess);
		this.setHandlerType(type);
	}

	public void act() {
		if (getHandlerType().contains(Types.LINK.strType)) {

		}
		if (getHandlerType().contains(Types.PRIVATE.strType)) {

		}
		if (getHandlerType().contains(Types.TWITTER.strType)) {

		}
		if (getHandlerType().contains(Types.FBPOST.strType)) {

		}

		System.out.println("Posted: " + getMessage() + " // "
				+ getHandlerType());
	}

	public String getHandlerType() {

		return handlerType;
	}

	private void setHandlerType(String handlerType) {
		this.handlerType = handlerType.toLowerCase();
	}

	public String getMessage() {
		return message;
	}

	private void setMessage(String message) {
		this.message = message;
	}

}
